<?php
/**
 * Created by PhpStorm.
 * User: tyrans@qq.com
 * Date: 2018/8/17
 * Time: 13:21
 */
namespace App\Handlers;

use App\Models\Wechat\ClientUserModel;
use EasyWeChat\Factory;
use EasyWeChat\Kernel\Contracts\EventHandlerInterface;
use EasyWeChat\Kernel\Messages\Transfer;
use Illuminate\Support\Facades\Log;

class WechatEventHandle implements EventHandlerInterface
{
    public function handle($payload = null)
    {
        switch ($payload['Event']) {
            case 'subscribe':
                return $this->onSubscribe($payload);
            case 'unsubscribe':
                return $this->unSubscribe($payload);
            case 'CLICK':
                if ($payload['EventKey'] === '_kefu_tel') {
                    return '如果您在使用过程中有任何问题，我们表示十分抱歉，我们将不断完善系统功能，优化使用体验。更多帮助请咨询客服热线：400-028-5151';
                }
                if ($payload['EventKey'] === '_kefu_online') {
                    $app = Factory::officialAccount(config('wechat.official_account'));
                    $kf_online_list = $app->customer_service->online()['kf_online_list'];
                    if (count($kf_online_list) === 0) {
                        return '十分抱歉呢，客服小姐姐和技术小哥哥现在都不在线哦。';
                    }
                    $app->customer_service->message('正在为您转接人工客服，请描述您在使用过程中遇到的问题...')
                        ->from('tfx_ztc@online')
                        ->to($payload['FromUserName'])
                        ->send();
                    return new Transfer();
                }
                return '【景区产品】 能将于 4月 正式上线，敬请期待。';
        }
    }

    public function onSubscribe($payload)
    {
        ClientUserModel::Subscribe($payload['FromUserName']);
        return '关注问候词';
    }

    public function unSubscribe($payload)
    {
        ClientUserModel::UnSubscribe($payload['FromUserName']);
        return 'ok';
    }
}